﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using EveryBookies.ViewModel;
using EveryBookies.Model;
using EveryBookies.Common;
using Microsoft.Practices.ServiceLocation;
using EveryBookies.View;

namespace EveryBookies.Controls
{
    public partial class FavoriteUserControl : UserControl
    {
        public BookiesViewModel bookiesViewModel
        {
            get
            {
                return (BookiesViewModel)DataContext;
            }
        }
        public ILocalData SaveDataWeb
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ILocalData>();
            }
        }
        bool isSave = true;
        DataSave offer = null;
        public FavoriteUserControl()
        {
            InitializeComponent();
        }

        public bool CloseWebView()
        {
            if (showWebViewOffer.Visibility == System.Windows.Visibility.Visible)
            {
                showWebViewOffer.Visibility = System.Windows.Visibility.Collapsed;
                showWebViewOffer.NavigateToString("");
                bookiesViewModel.IsShowFavoriteLoading = false;
                btBack.Visibility = System.Windows.Visibility.Collapsed;
                isSave = false;
                return true;

            }
            return false;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //bookiesViewModel.GetFavorites();
        }

        private void loadComplete(object sender, NavigationEventArgs e)
        {
            if (isSave)
            {
                var getURi = e.Uri;
                SaveDataWeb.SaveData(new DataSave() { id = offer.id, link = getURi.ToString() }, string.Format("FavId{0}", offer.id));
                bookiesViewModel.IsShowFavoriteLoading = false;
            }
            
        }

        private void startLoad(object sender, NavigatingEventArgs e)
        {
            bookiesViewModel.IsShowFavoriteLoading = true;
        }

        private async void selectItemOffer(object sender, System.Windows.Input.GestureEventArgs e)
        {
            btBack.Visibility = System.Windows.Visibility.Visible;
            bookiesViewModel.IsShowFavoriteLoading = true;
            var getData = ((Grid)sender).DataContext as Offer;
            if (getData != null)
            {
                offer = new DataSave() { id = getData.id, link = getData.link };

                var getDataSave = await SaveDataWeb.LoadData<DataSave>(string.Format("FavId{0}", offer.id));
                if (getDataSave != null)
                {
                    if (!string.IsNullOrEmpty(getDataSave.link))
                    {
                        showWebViewOffer.Source = new Uri(getDataSave.link);
                    }
                    else
                    {
                        showWebViewOffer.Source = new Uri(offer.link);
                    }
                }
                else
                {
                    showWebViewOffer.Source = new Uri(offer.link);
                }
                isSave = true;
                showWebViewOffer.Visibility = Visibility.Visible;
            }

        }

        private void LongListSelector_ItemRealized(object sender, ItemRealizationEventArgs e)
        {

        }

        private void loadFail(object sender, NavigationFailedEventArgs e)
        {
            bookiesViewModel.IsShowFavoriteLoading = false;
        }

        private void backbt(object sender, RoutedEventArgs e)
        {
            CloseWebView();

        }

        private void backHome(object sender, RoutedEventArgs e)
        {
            var currentPage=    ((PhoneApplicationFrame)Application.Current.RootVisual).Content as MainPage;
            currentPage.OffersClick();
        }

    }
}
